﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Entity;

public partial class Admin_Danh_muc_san_pham : System.Web.UI.Page
{
    TblGroupDAO groupDAO = new TblGroupDAO(Global.CONNECTION_STRING);
    TblCategoriesDAO cateDAO = new TblCategoriesDAO(Global.CONNECTION_STRING);

    protected void Page_Load(object sender, EventArgs e)
    {
        string id = Request.QueryString["id"];
        if (!IsPostBack)
        {
            loadGroup();
            LoadCategories();
            if (id != null || id != "")
            {
                LoadDetail_Categories();
            }
        }
    }

    //load Danh sách Categories
    public void LoadCategories()
    {
        grvCate.DataSource = cateDAO.GetAll();
        grvCate.DataBind();
    }

    //Load Group
    private void loadGroup()
    {
        drGroup.DataSource = groupDAO.GetAll();
        drGroup.DataTextField = "GroupName";
        drGroup.DataValueField = "IDGroup";
        drGroup.DataBind();
    }

    // load chi tiết categories
    private void LoadDetail_Categories()
    {
        loadGroup();
        string id = Request.QueryString["id"];
        DataTable cate = cateDAO.GetCategories_ByID(Global.ConvertToInt(id));
        foreach (DataRow dr in cate.Rows)
        {
            txtCateName.Text = dr["CateName"].ToString();
            txtCateNameDescription.Text = dr["CateDescription"].ToString();
            drGroup.SelectedValue = Convert.ToInt32(dr["IDGroup"]).ToString();
            drLang.SelectedValue = Convert.ToInt32(dr["IdLang"]).ToString();
            drLoai.SelectedValue = Convert.ToInt32(dr["IdPhanLoai"]).ToString();

        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string id = Request.QueryString["id"];
        bool flag = false;

        try
        {
            TblCategories cate = new TblCategories();
            cate.IDGroup = Convert.ToInt32 (drGroup.SelectedValue);
            cate.ImageURL = "";
            cate.CateName = txtCateName.Text;
            cate.CateDescription = txtCateNameDescription.Text;
            cate.IdPhanLoai = Convert.ToInt32(drLoai.SelectedValue);
            cate.IdLang = Convert.ToInt32(drLang.SelectedValue);

            if (id == "" || id == null)
            {
                cateDAO.InsUp(cate, 1);
            }
            else
            {
                cate.CateID = Global.ConvertToInt(id);
                cateDAO.InsUp(cate, 2);

            }
            flag = true;


        }
        catch (Exception ex)
        {
            throw ex;
        }
        if (flag)
        {
            Response.Redirect("~/Admin/Danh-muc-san-pham.aspx");
        }
    }
}
